package com.mainworks.commons.impl;

import com.mainworks.commons.entity.User;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
@Data
public class UserDetailed implements UserDetails {
    private User user;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return user == null?"":user.getUserName();
    }

    /**
     * 账户是否过期  is_expired
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        long curren=System.currentTimeMillis();
        if (user!=null&&curren > user.getPasswordOverdue()){
            return true;
        }
        return user == null?true:user.getIsExpired()==1?true:false;
    }

    /**
     * 账号是否被锁定 is_locked
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        long curren=System.currentTimeMillis();
        if (user!=null&&curren > user.getPasswordOverdue()){
            return true;
        }
        return user == null?true:user.getIsLocked()==1?true:false;
    }

    /**
     * 凭证是否过期 （token是否过期）
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        // boolean token = false;

        return false;
    }

    /**
     * 是否启用 is_enabled
     * @return
     */
    @Override
    public boolean isEnabled() {
        return user == null?true:user.getIsEnabled()==1?true:false;
    }
}
